/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/**
 *
 * @author Administrator
 */
public class MenuCanvas extends GameCanvas implements Runnable {

    //midlet
    private NinjaRunMidlet midlet;
    private Display display;
    //path
    private String pathShadow = "";
    public String pathBackground = "";
    public static int background = 0;
    private String pathTitle = "";
    private String pathPlayOff = "";
    private String pathPlayOn = "";
    private String pathHelpOff = "";
    private String pathHelpOn = "";
    private String pathAboutOff = "";
    private String pathAboutOn = "";
    private String pathQuitOn = "";
    private String pathBack = "";
    //path level
    private String pathGrassUnLockedOff = "";
    private String pathGrassUnLockedOn = "";
    private String pathDessertLockedOff = "";
    private String pathDessertLockedOn = "";
    private String pathDessertUnLockedOff = "";
    private String pathDessertUnLockedOn = "";
    private String pathSnowLockedOff = "";
    private String pathSnowLockedOn = "";
    private String pathSnowUnLockedOff = "";
    private String pathSnowUnLockedOn = "";
    //images        
    private Image imgTitle;
    private Image imgPlayOff;
    private Image imgPlayOn;
    private Image imgHelpOff;
    private Image imgHelpOn;
    private Image imgAboutOff;
    private Image imgAboutOn;
    private Image imgQuitSdOn;
    private int btnImgMenuWidth;
    private int btnImgMenuHeight;
    private Image imgGrassUnLockedOff;
    private Image imgGrassUnLockedOn;
    private Image imgDessertLockedOff;
    private Image imgDessertLockedOn;
    private Image imgDessertUnLockedOff;
    private Image imgDessertUnLockedOn;
    private Image imgSnowLockedOff;
    private Image imgSnowLockedOn;
    private Image imgSnowUnLockedOff;
    private Image imgSnowUnLockedOn;
    private Image imgBack;
    private int btnImgLevelWidth;
    private int btnImgLevelHeight;
    //button
    ButtonImage btnPlay;
    ButtonImage btnHelp;
    ButtonImage btnAbout;
    ButtonImage btnQuit;
    ButtonImage btnGrass;
    ButtonImage btnDessert;
    ButtonImage btnSnow;
    ButtonImage btnBack;
    //font
    public static Font SMALL_PLAIN_FONT;
    public static Font SMALL_BOLD_FONT;
    public static Font LARGE_BOLD_FONT;
    //canvas property
    private int CANVAS_WIDTH = getWidth();
    private int CANVAS_HEIGHT = getHeight();
    private int bgWidth = 0;
    private int warna;
    //positions
    private int x = 0;
    private int y = 0;
    private int xBg = 0;
    private int yBg = 0;
    private int xShadow = 0;
    private int yShadow = 0;
    private int jarakLevel = 0;
    private int xyArea[] = new int[4];
    private int xHelp, yHelp, xPlay, yPlay, xAbout, yAbout;
    private int xQuit, yQuit, xGrass, yGrass, xDessert, yDessert, xSnow, ySnow;
    private int acuanMenu, acuanLevel;
    private int xBack, yBack;
    //animated sprite
    private AnimatedSprite spShadow;
    private static int FRAME_SHADOW_WIDTH = 0;
    private static int FRAME_SHADOW_HEIGHT = 0;
    private boolean running = false;
    private BackgroundScroll bgScroll;
    private LayerManager lmgr;
    //speed 
    private int speedGame = 50;
    private int speedBg = 5;
    private int speedGeser = 50;
    //boolean value
    private boolean isGeserKeLevel = false;
    private boolean isGeserKeMenu = false;
    private boolean isGeserBack = false;

    public MenuCanvas(NinjaRunMidlet midlet, Display display) {
        super(true);
        setFullScreenMode(true);

        this.midlet = midlet;
        this.display = display;
        
        CANVAS_WIDTH = getWidth();
        CANVAS_HEIGHT = getHeight();

        jarakLevel = (int) (CANVAS_WIDTH * 0.9);
        warna = 0xc6eeff;
        
        if (CANVAS_WIDTH < CANVAS_HEIGHT) {
            //portrait
            yBg = CANVAS_HEIGHT - 240;
        } else {
            //landscape
            yBg = 0;
        }
         
        
        if (CANVAS_WIDTH == 640) {
            FRAME_SHADOW_WIDTH = 46;
            FRAME_SHADOW_HEIGHT = 49;
            pathShadow = "/black-shadow640.png";
            pathBackground = "/bg-snow640.png";
            pathPlayOff = "/btnPlayOff640.png";
            pathPlayOn = "/btnPlayOn640.png";
            pathHelpOff = "/btnHelpOff640.png";
            pathHelpOn = "/btnHelpOn640.png";
            pathAboutOff = "/btnAboutOff640.png";
            pathAboutOn = "/btnAboutOn640.png";
            pathQuitOn = "/close640.png";
            pathTitle = "/titleNinjaRun640.png";
            btnImgMenuWidth = 200;
            btnImgMenuHeight = 50;
            btnImgLevelWidth = 108;
            btnImgLevelHeight = 108;
            bgWidth = 1280;
            pathGrassUnLockedOff = "/grassUnlockedOff640.png";
            pathGrassUnLockedOn = "/grassUnlockedOn640.png";
            pathDessertLockedOff = "/dessertLockedOff640.png";
            pathDessertLockedOn = "/dessertLockedOn640.png";
            pathDessertUnLockedOff = "/dessertUnlockedOff640.png";
            pathDessertUnLockedOn = "/dessertUnlockedOn640.png";
            pathSnowLockedOff = "/snowLockedOff640.png";
            pathSnowLockedOn = "/snowLockedOn640.png";
            pathSnowUnLockedOff = "/snowUnockedOff640.png";
            pathSnowUnLockedOn = "/snowUnockedOn640.png";
            pathBack = "/btnBack.png";
        } else {
            FRAME_SHADOW_WIDTH = 23;
            FRAME_SHADOW_HEIGHT = 25;
            pathShadow = "/black-shadow320.png";
            pathBackground = "/bg-snow320.png";
            pathPlayOff = "/btnPlayOff320.png";
            pathPlayOn = "/btnPlayOn320.png";
            pathHelpOff = "/btnHelpOff320.png";
            pathHelpOn = "/btnHelpOn320.png";
            pathAboutOff = "/btnAboutOff320.png";
            pathAboutOn = "/btnAboutOn320.png";
            pathQuitOn = "/close320.png";
            pathTitle = "/titleNinjaRun320.png";
            btnImgMenuWidth = 100;
            btnImgMenuHeight = 25;
            btnImgLevelWidth = 54;
            btnImgLevelHeight = 54;
            bgWidth = 640;
            pathGrassUnLockedOff = "/grassUnlockedOff320.png";
            pathGrassUnLockedOn = "/grassUnlockedOn320.png";
            pathDessertLockedOff = "/dessertLockedOff320.png";
            pathDessertLockedOn = "/dessertLockedOn320.png";
            pathDessertUnLockedOff = "/dessertUnlockedOff320.png";
            pathDessertUnLockedOn = "/dessertUnlockedOn320.png";
            pathSnowLockedOff = "/snowLockedOff320.png";
            pathSnowLockedOn = "/snowLockedOn320.png";
            pathSnowUnLockedOff = "/snowUnockedOff320.png";
            pathSnowUnLockedOn = "/snowUnockedOn320.png";
            pathBack = "/btnBack.png";
        }

        try {
            bgScroll = new BackgroundScroll(Image.createImage(pathBackground));
            bgScroll.setRefPixelPosition(xBg, yBg);
            //animated sprite
            spShadow = new AnimatedSprite(Image.createImage(pathShadow),
                    FRAME_SHADOW_WIDTH, FRAME_SHADOW_HEIGHT);
            spShadow.defineReferencePixel(FRAME_SHADOW_WIDTH / 2, FRAME_SHADOW_HEIGHT / 2);

            lmgr = new LayerManager();
            lmgr.append(spShadow);
            lmgr.append(bgScroll);
        } catch (IOException ex) {
        }

        try {
            imgTitle = Image.createImage(pathTitle);
            imgBack = Image.createImage(pathBack);

            imgPlayOff = Image.createImage(pathPlayOff);
            imgPlayOn = Image.createImage(pathPlayOn);
            imgHelpOff = Image.createImage(pathHelpOff);
            imgHelpOn = Image.createImage(pathHelpOn);
            imgAboutOff = Image.createImage(pathAboutOff);
            imgAboutOn = Image.createImage(pathAboutOn);

            imgQuitSdOn = Image.createImage(pathQuitOn);

            imgGrassUnLockedOff = Image.createImage(pathGrassUnLockedOff);
            imgGrassUnLockedOn = Image.createImage(pathGrassUnLockedOn);
            imgDessertLockedOff = Image.createImage(pathDessertLockedOff);
            imgDessertLockedOn = Image.createImage(pathDessertLockedOn);
            imgDessertUnLockedOff = Image.createImage(pathDessertUnLockedOff);
            imgDessertUnLockedOn = Image.createImage(pathDessertUnLockedOn);
            imgSnowLockedOff = Image.createImage(pathSnowLockedOff);
            imgSnowLockedOn = Image.createImage(pathSnowLockedOn);
            imgSnowUnLockedOff = Image.createImage(pathSnowUnLockedOff);
            imgSnowUnLockedOn = Image.createImage(pathSnowUnLockedOn);

        } catch (IOException ex) {
        }

        SMALL_PLAIN_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        SMALL_BOLD_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        LARGE_BOLD_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);


        xHelp = (int) (getWidth() * 0.25) - (btnImgMenuWidth / 2);
        yHelp = (int) (getHeight() * 0.25) - (btnImgMenuHeight / 2);
        xPlay = (int) (getWidth() * 0.5) - (btnImgMenuWidth / 2);
        yPlay = (int) (getHeight() * 0.5) - (btnImgMenuHeight / 2);
        xAbout = (int) (getWidth() * 0.75) - (btnImgMenuWidth / 2);
        yAbout = (int) (getHeight() * 0.75) - (btnImgMenuHeight / 2);
        xQuit = (int) getWidth() - imgQuitSdOn.getWidth();
        yQuit = 0;
        acuanMenu = xHelp;

        xGrass = (int) (getWidth() * 0.35) - (btnImgLevelWidth / 2) + jarakLevel;
        yGrass = (int) (getHeight() * 0.35) - (btnImgLevelHeight / 2);
        xDessert = (int) (getWidth() * 0.65) - (btnImgMenuWidth / 2) + jarakLevel;
        yDessert = (int) (getHeight() * 0.5) - (btnImgMenuHeight / 2);
        xSnow = (int) (getWidth() * 0.85) - (btnImgMenuWidth / 2) + jarakLevel;
        ySnow = (int) (getHeight() * 0.75) - (btnImgMenuHeight / 2);
        acuanLevel = (int) (getWidth() * 0.35) - (btnImgLevelWidth / 2);

        xBack = 0;//getWidth() - imgMineIdOff.getWidth();
        yBack = getHeight() - imgBack.getHeight();

        //System.out.println("menu " + acuanMenu + " level " + acuanLevel);

        xyArea[0] = yHelp; //x1
        xyArea[1] = yHelp + imgHelpOff.getWidth(); //x2
        xyArea[2] = xHelp; //y1
        xyArea[3] = xHelp + imgHelpOff.getHeight(); //y2
        btnHelp = new ButtonImage("help", imgHelpOff, imgHelpOn,
                imgHelpOff, xHelp,
                yHelp, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = yPlay; //x1
        xyArea[1] = yPlay + imgPlayOff.getWidth(); //x2
        xyArea[2] = xPlay; //y1
        xyArea[3] = xPlay + imgPlayOff.getHeight(); //y2
        btnPlay = new ButtonImage("play", imgPlayOff, imgPlayOn,
                imgPlayOff, xPlay,
                yPlay, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = yAbout; //x1
        xyArea[1] = yAbout + btnImgMenuWidth; //x2
        xyArea[2] = xAbout; //y1
        xyArea[3] = xAbout + btnImgMenuHeight; //y2
        btnAbout = new ButtonImage("about", imgAboutOff, imgAboutOn,
                imgAboutOff, xAbout,
                yAbout, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = yQuit; //x1
        xyArea[1] = yQuit + imgQuitSdOn.getWidth(); //x2
        xyArea[2] = xQuit; //y1
        xyArea[3] = xQuit + imgQuitSdOn.getHeight(); //y2
        btnQuit = new ButtonImage("quit", imgQuitSdOn, imgQuitSdOn,
                imgQuitSdOn, xQuit,
                yQuit, xyArea, Graphics.TOP | Graphics.LEFT);

        btnPlay.setIsPressed(true);

        xyArea[0] = yGrass; //x1
        xyArea[1] = yGrass + btnImgLevelWidth; //x2
        xyArea[2] = xGrass; //y1
        xyArea[3] = xGrass + btnImgMenuHeight; //y2
        btnGrass = new ButtonImage("grass", imgGrassUnLockedOff, imgGrassUnLockedOn,
                imgGrassUnLockedOff, xGrass,
                yGrass, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = yDessert; //x1
        xyArea[1] = yDessert + btnImgLevelWidth; //x2
        xyArea[2] = xDessert; //y1
        xyArea[3] = xDessert + btnImgMenuHeight; //y2
        btnDessert = new ButtonImage("dessert", imgDessertLockedOff, imgDessertLockedOn,
                imgDessertLockedOff, xDessert,
                yDessert, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = ySnow; //x1
        xyArea[1] = ySnow + btnImgLevelWidth; //x2
        xyArea[2] = xSnow; //y1
        xyArea[3] = xSnow + btnImgMenuHeight; //y2
        btnSnow = new ButtonImage("snow", imgSnowLockedOff, imgSnowLockedOn,
                imgSnowLockedOff, xSnow,
                ySnow, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = yBack; //x1
        xyArea[1] = yBack + imgBack.getWidth(); //x2
        xyArea[2] = xBack; //y1
        xyArea[3] = xBack + imgBack.getHeight(); //y2
        btnBack = new ButtonImage("back", imgBack, imgBack,
                imgBack, xBack,
                yBack, xyArea, Graphics.TOP | Graphics.LEFT);


        xShadow = (int) ((CANVAS_WIDTH * 0.2));
        yShadow = (int) ((CANVAS_HEIGHT * 0.8));
        btnGrass.setIsPressed(true);
        
        spShadow.setRefPixelPosition(xShadow, yShadow);
        
        System.out.println(CANVAS_WIDTH + "x" + CANVAS_HEIGHT);

    }
    //start thread

    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    //game loop
    public void run() {
        Graphics g = getGraphics();
        while (running) {
            // look for keypress
            checkForKeys();
            //checkForCollision();

            if (isGeserKeLevel) {
                if (xGrass < acuanLevel) {
                    isGeserKeLevel = false;
                    isGeserBack = true;
                }
            } else if (isGeserKeMenu) {
                if (xHelp > acuanMenu) {
                    isGeserKeMenu = false;
                    xHelp = xHelp - speedGeser;
                    xPlay = xPlay - speedGeser;
                    xAbout = xAbout - speedGeser;
                    xGrass = xGrass - speedGeser;
                    xDessert = xDessert - speedGeser;
                    xSnow = xSnow - speedGeser;

                    btnHelp.setPositionXY(xHelp, yHelp, Graphics.TOP | Graphics.LEFT);
                    btnPlay.setPositionXY(xPlay, yPlay, Graphics.TOP | Graphics.LEFT);
                    btnAbout.setPositionXY(xAbout, yAbout, Graphics.TOP | Graphics.LEFT);

                    btnGrass.setPositionXY(xGrass, yGrass, Graphics.TOP | Graphics.LEFT);
                    btnDessert.setPositionXY(xDessert, yDessert, Graphics.TOP | Graphics.LEFT);
                    btnSnow.setPositionXY(xSnow, ySnow, Graphics.TOP | Graphics.LEFT);
                }
            }
            //System.out.println("xHelp : " + xHelp
            //        + " acuanMenu : " + acuanMenu);

            drawDisplay(g);

            //System.out.println(spShuriken.getX());
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }
    }

    public void stop() {
        running = false;
    }

    /*
    protected void sizeChanged(int i, int i1) {
    super.sizeChanged(i, i1);
    //System.out.println("Oit!");
    System.out.println(getWidth() + "x" +getHeight());
    }
     *
     */
    protected void drawDisplay(Graphics g) {

        g.setColor(warna);
        g.fillRect(0, 0, getWidth(), getHeight());

        if (xBg == -bgWidth) {
            xBg = 0;
        } else {
            xBg = xBg - speedBg;
        }
        bgScroll.setPosition(xBg, yBg);
        spShadow.nextFrame();
        lmgr.paint(g, 0, 0);

        g.drawImage(imgTitle, CANVAS_WIDTH / 2 - (imgTitle.getWidth() / 2), 0, Graphics.TOP | Graphics.LEFT);

        //paint disini
        //sampai sini        

        //flush off-screen buffer to display

        if (isGeserKeLevel) {
            xHelp = xHelp - speedGeser;
            xPlay = xPlay - speedGeser;
            xAbout = xAbout - speedGeser;
            xGrass = xGrass - speedGeser;
            xDessert = xDessert - speedGeser;
            xSnow = xSnow - speedGeser;

            btnHelp.setPositionXY(xHelp, yHelp, Graphics.TOP | Graphics.LEFT);
            btnPlay.setPositionXY(xPlay, yPlay, Graphics.TOP | Graphics.LEFT);
            btnAbout.setPositionXY(xAbout, yAbout, Graphics.TOP | Graphics.LEFT);

            btnGrass.setPositionXY(xGrass, yGrass, Graphics.TOP | Graphics.LEFT);
            btnDessert.setPositionXY(xDessert, yDessert, Graphics.TOP | Graphics.LEFT);
            btnSnow.setPositionXY(xSnow, ySnow, Graphics.TOP | Graphics.LEFT);
        } else if (isGeserBack) {
            //btnBack
            btnBack.paint(g);
        } else if (isGeserKeMenu) {
            xHelp = xHelp + speedGeser;
            xPlay = xPlay + speedGeser;
            xAbout = xAbout + speedGeser;
            xGrass = xGrass + speedGeser;
            xDessert = xDessert + speedGeser;
            xSnow = xSnow + speedGeser;

            btnHelp.setPositionXY(xHelp, yHelp, Graphics.TOP | Graphics.LEFT);
            btnPlay.setPositionXY(xPlay, yPlay, Graphics.TOP | Graphics.LEFT);
            btnAbout.setPositionXY(xAbout, yAbout, Graphics.TOP | Graphics.LEFT);

            btnGrass.setPositionXY(xGrass, yGrass, Graphics.TOP | Graphics.LEFT);
            btnDessert.setPositionXY(xDessert, yDessert, Graphics.TOP | Graphics.LEFT);
            btnSnow.setPositionXY(xSnow, ySnow, Graphics.TOP | Graphics.LEFT);
        }
        btnPlay.paint(g);
        btnAbout.paint(g);
        btnHelp.paint(g);
        btnQuit.paint(g);

        btnGrass.paint(g);
        btnDessert.paint(g);
        btnSnow.paint(g);
        flushGraphics();
    }

    private void checkForKeys() {
        int keyState = getKeyStates();

        if ((keyState & FIRE_PRESSED) != 0) {
            //isAttack = true;
            keyPressed(-5);
            //System.out.println(getKeyStates());
        } else if ((keyState & LEFT_PRESSED) != 0) {
            keyPressed(-3);
            //System.out.println(getKeyStates());
        } else if ((keyState & RIGHT_PRESSED) != 0) {
            keyPressed(-4);
            //System.out.println(getKeyStates());
        } else if ((keyState & UP_PRESSED) != 0) {
            keyPressed(-1);
            //System.out.println(getKeyStates());
        } else if ((keyState & DOWN_PRESSED) != 0) {
            keyPressed(-2);
            //System.out.println(getKeyStates());
        } else if ((keyState & GAME_A_PRESSED) != 0) {
            //System.out.println("A!!!");
        } else if ((keyState & GAME_B_PRESSED) != 0) {
            //System.out.println("B!!!");
        } else if ((keyState & GAME_C_PRESSED) != 0) {
            //System.out.println("C!!!");
        } else if ((keyState & GAME_D_PRESSED) != 0) {
            //System.out.println("D!!!");
        }
    }

    protected void keyPressed(int keyCode) {
        try {
            //System.out.println(keyCode);
            Thread.sleep(speedGame * 2);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        if (keyCode == -5) {
            if (btnQuit.isIsPressed()) {
                midlet.exit();
            }
            if (isGeserBack) {
                if (btnGrass.isIsPressed()) {
                    midlet.showGrassCanvas();
                } else if (btnDessert.isIsPressed()) {
                    midlet.showDessertCanvas();
                } else if (btnSnow.isIsPressed()) {
                    midlet.showSnowCanvas();
                }
            } else {
                if (btnPlay.isIsPressed()) {
                    isGeserKeLevel = true;
                } else if (btnAbout.isIsPressed()) {
                    midlet.showAboutCanvas();
                } else if (btnHelp.isIsPressed()) {
                    midlet.showHelpCanvas();
                }
            }

        } else if (keyCode == -2 || keyCode == -4) {
            //pencet bawah /kanan
            if (btnHelp.isIsPressed()) {
                btnHelp.setIsNothing(true);
                btnHelp.setIsPressed(false);
                btnPlay.setIsPressed(true);
            } else if (btnPlay.isIsPressed()) {
                btnPlay.setIsNothing(true);
                btnPlay.setIsPressed(false);
                btnAbout.setIsPressed(true);
            } else if (btnAbout.isIsPressed()) {
                btnAbout.setIsNothing(true);
                btnAbout.setIsPressed(false);
                btnHelp.setIsPressed(true);
            }

            //menu
            if (btnGrass.isIsPressed()) {
                btnGrass.setIsNothing(true);
                btnGrass.setIsPressed(false);
                btnDessert.setIsPressed(true);
                System.out.println("grass oi");
            } else if (btnDessert.isIsPressed()) {
                btnDessert.setIsNothing(true);
                btnDessert.setIsPressed(false);
                btnSnow.setIsPressed(true);
                System.out.println("dessert oi");
            } else if (btnSnow.isIsPressed()) {
                btnSnow.setIsNothing(true);
                btnSnow.setIsPressed(false);
                btnGrass.setIsPressed(true);
                System.out.println("snow oi");
            }
        } else if (keyCode == -1 || keyCode == -3) {
            //pencet atas / kiri
            if (btnHelp.isIsPressed()) {
                btnHelp.setIsNothing(true);
                btnHelp.setIsPressed(false);
                btnAbout.setIsPressed(true);
            } else if (btnPlay.isIsPressed()) {
                btnPlay.setIsNothing(true);
                btnPlay.setIsPressed(false);
                btnHelp.setIsPressed(true);
            } else if (btnAbout.isIsPressed()) {
                btnAbout.setIsNothing(true);
                btnAbout.setIsPressed(false);
                btnPlay.setIsPressed(true);
            }

            if (btnGrass.isIsPressed()) {
                btnGrass.setIsNothing(true);
                btnGrass.setIsPressed(false);
                btnSnow.setIsPressed(true);
            } else if (btnDessert.isIsPressed()) {
                btnDessert.setIsNothing(true);
                btnDessert.setIsPressed(false);
                btnGrass.setIsPressed(true);
            } else if (btnSnow.isIsPressed()) {
                btnSnow.setIsNothing(true);
                btnSnow.setIsPressed(false);
                btnDessert.setIsPressed(true);
            }
        } else if (keyCode == -7) {
            midlet.exit();
        } else if (keyCode == -6) {
            isGeserBack = false;
            isGeserKeMenu = true;
        }
        repaint();
    }

    protected void pointerPressed(int x, int y) {
        //recognizer = new GestureRecognizer(x, y);
        this.x = x;
        this.y = y;

        //show = false;
        if (x > btnHelp.xImage
                && x < (btnHelp.xImage + btnHelp.imageWidth)
                && y > btnHelp.yImage
                && y < (btnHelp.yImage + btnHelp.imageHeight)) {
            btnHelp.pointerPressed(x, y);
            repaint();
        } else if (x > btnAbout.xImage
                && x < (btnAbout.xImage + btnAbout.imageWidth)
                && y > btnAbout.yImage
                && y < (btnAbout.yImage + btnAbout.imageHeight)) {
            btnAbout.pointerPressed(x, y);
            repaint();
        } else if (x > btnPlay.xImage
                && x < (btnPlay.xImage + btnPlay.imageWidth)
                && y > btnPlay.yImage
                && y < (btnPlay.yImage + btnPlay.imageHeight)) {
            btnPlay.pointerPressed(x, y);
            repaint();
        }

    }

    protected void pointerReleased(int x, int y) {
        //recognizer.endGesture(x, y);
        this.x = x;
        this.y = y;
        //show = true;
        if (x > btnHelp.xImage
                && x < (btnHelp.xImage + btnHelp.imageWidth)
                && y > btnHelp.yImage
                && y < (btnHelp.yImage + btnHelp.imageHeight)) {
            btnHelp.pointerReleased(x, y);

            btnHelp.setIsPressed(true);
            btnAbout.setIsNothing(true);
            btnAbout.setIsPressed(false);
            btnPlay.setIsNothing(true);
            btnPlay.setIsPressed(false);
            btnHelp.setIsPressed(true);

            keyPressed(-5);
        } else if (x > btnAbout.xImage
                && x < (btnAbout.xImage + btnAbout.imageWidth)
                && y > btnAbout.yImage
                && y < (btnAbout.yImage + btnAbout.imageHeight)) {
            btnAbout.pointerReleased(x, y);
            btnAbout.setIsPressed(true);

            btnPlay.setIsNothing(true);
            btnPlay.setIsPressed(false);
            btnHelp.setIsNothing(true);
            btnHelp.setIsPressed(false);
            keyPressed(-5);
        } else if (x > btnPlay.xImage
                && x < (btnPlay.xImage + btnPlay.imageWidth)
                && y > btnPlay.yImage
                && y < (btnPlay.yImage + btnPlay.imageHeight)) {
            btnPlay.pointerReleased(x, y);
            btnPlay.setIsPressed(true);

            btnAbout.setIsNothing(true);
            btnAbout.setIsPressed(false);
            btnHelp.setIsNothing(true);
            btnHelp.setIsPressed(false);

            keyPressed(-5);
        } else if (x > btnGrass.xImage
                && x < (btnGrass.xImage + btnGrass.imageWidth)
                && y > btnGrass.yImage
                && y < (btnGrass.yImage + btnGrass.imageHeight)) {
            btnGrass.pointerReleased(x, y);
            btnGrass.setIsPressed(true);

            btnDessert.setIsNothing(true);
            btnDessert.setIsPressed(false);
            btnSnow.setIsNothing(true);
            btnSnow.setIsPressed(false);

            keyPressed(-5);
        } else if (x > btnDessert.xImage
                && x < (btnDessert.xImage + btnDessert.imageWidth)
                && y > btnDessert.yImage
                && y < (btnDessert.yImage + btnDessert.imageHeight)) {
            btnDessert.pointerReleased(x, y);
            btnDessert.setIsPressed(true);

            btnSnow.setIsNothing(true);
            btnSnow.setIsPressed(false);
            btnGrass.setIsNothing(true);
            btnGrass.setIsPressed(false);

            keyPressed(-5);
        } else if (x > btnSnow.xImage
                && x < (btnSnow.xImage + btnSnow.imageWidth)
                && y > btnSnow.yImage
                && y < (btnSnow.yImage + btnSnow.imageHeight)) {
            btnSnow.pointerReleased(x, y);
            btnSnow.setIsPressed(true);

            btnDessert.setIsNothing(true);
            btnDessert.setIsPressed(false);
            btnGrass.setIsNothing(true);
            btnGrass.setIsPressed(false);

            keyPressed(-5);
        } else if (x > btnQuit.xImage
                && x < (btnQuit.xImage + btnQuit.imageWidth)
                && y > btnQuit.yImage
                && y < (btnQuit.yImage + btnQuit.imageHeight)) {
            keyPressed(-7);
        } else if (x > btnBack.xImage
                && x < (btnBack.xImage + btnBack.imageWidth)
                && y > btnBack.yImage
                && y < (btnBack.yImage + btnBack.imageHeight)) {
            isGeserBack = false;
            isGeserKeMenu = true;
        }
        repaint();
    }
}
